package com.jpsl.core.reflection.property.m;

import com.jpsl.core.reflection.property.AccessException;
import com.jpsl.core.reflection.property.PropertyValueGetter;

import java.lang.reflect.Method;

/**
 * @author 懒王猫
 */
class ByMethodPropertyValueGetter implements PropertyValueGetter {

    // not method name
    private final String propertyName;

    private final Method getter;

    ByMethodPropertyValueGetter(String propertyName, Method getter) {
        this.propertyName = propertyName;
        this.getter = getter;
        this.getter.setAccessible(true);
    }

    @Override
    public String getName() {
        return propertyName;
    }

    @Override
    public Class<?> getType() {
        return getter.getReturnType();
    }

    @Override
    public Object getValue(Object bean) throws AccessException {
        try {
            return getter.invoke(bean);
        } catch (ReflectiveOperationException e) {
            throw new AccessException(e);
        }
    }

}
